<?php

class menu_controller extends common {
	const fields = array('pId' => '父菜单', 'title' => '标题', 'icon' => '图标', 'href' => '链接', 'target' => '跳转方式', 'order' => '排序', 'status' => '状态');
	
	function menu_list() {
		$tableName = 'admin_menu';
		
		filter_post(self::fields, true);
		
		$wheres = array();
		$where0 = "`pId` IS NULL";
		foreach ($_POST as $key => $value) {
			if (in_array($key, array(
				'pId',
				'order',
				'status'
			)) && is_numeric($value)) {
				if ($key === 'pId')
					$where0 = "`pId` = {$value}";
				else
					$wheres[] = "a.`{$key}` = {$value}";
			} else
				$wheres[] = "a.`{$key}` LIKE '%{$value}%'";
		}
		$where = count($wheres) > 0 ? implode(' AND ', $wheres) : '1';
		
		$cte = trim("
			WITH RECURSIVE `cte` AS (
				SELECT * FROM `{$tableName}` WHERE {$where0}
				UNION ALL
				SELECT a.* FROM `{$tableName}` a
				INNER JOIN `cte` t on a.`pId` = t.`id`
				WHERE {$where}
			)
		");
		
		if (false === $rows = $this->sql->cte($cte)->order(array(
			'pId' => 'ASC',
			'order' => 'ASC'
		))->select('cte'))
			return msg(1, '获取数据失败！');
		
		return msg(0, null, array(
			'count' => $this->sql->cte($cte)->count('cte'),
			'data' => array2tree($rows, isset($_POST['pId']) && is_numeric($_POST['pId']) ? array(
				'root' => intval($_POST['pId'])
			) : null)
		));
	}
	
	function menu_list_title() {
		$tableName = 'admin_menu';
		
		$where0 = "`pId` IS NULL";
		$where  = '1';
		if (!empty($_POST['id']) && is_numeric($_POST['id'])) {
			$where0 .= " AND `id` <> {$_POST['id']}";
			$where .= " AND a.`id` <> {$_POST['id']}";
		}
		$cte = trim("
			WITH RECURSIVE `cte` AS (
				SELECT * FROM `{$tableName}` WHERE {$where0}
				UNION ALL
				SELECT a.* FROM `{$tableName}` a
				INNER JOIN `cte` t on a.`pId` = t.`id`
				WHERE {$where}
			)
		");
		
		if (false === $rows = $this->sql->cte($cte)->field(array(
			'id',
			'pId',
			'title'
		))->order(array(
			'pId' => 'ASC',
			'order' => 'ASC'
		))->select('cte'))
			return msg(1, '获取数据失败！');
		
		return msg(0, null, array(
			'data' => array2tree($rows)
		));
	}
	
	// 添加
	function add() {
		$tableName = 'admin_menu';
		
		filter_post(self::fields, true);
		
		if (!isset($_POST['title']))
			return msg(1, '参数非法！');
		
		$_POST['pId'] = isset($_POST['pId']) && is_numeric($_POST['pId']) ? $_POST['pId'] : null;
		
		if ($res = $this->sql->field('id')->where(array(
			'pId' => $_POST['pId'],
			'title' => $_POST['title']
		))->select($tableName, $_POST))
			return msg(2, '添加失败，已存在同级菜单！');
		
		if (!$res = $this->sql->insert($tableName, $_POST))
			return msg(3, '添加失败！');
		
		$this->log('添加菜单：' . $_POST['title']);
		
		return msg(0, '添加成功！', array(
			'parent' => true
		));
	}
	
	// 修改
	function update() {
		$tableName = 'admin_menu';
		$ids       = $_POST['id'];
		
		$items = filter_post(self::fields);
		
		if (empty($ids) || !preg_match('/^\s*\d+\s*(,\s*\d+\s*)*$/', $ids) || count($_POST) < 1)
			return msg(1, '参数非法！');
		
		if (!$res = $this->sql->where("id IN ({$ids})")->update($tableName, $_POST))
			return msg(2, '修改失败！');
		
		$values = count($_POST) > 1 ? json($_POST, false) : array_shift($_POST);
		$this->log('修改菜单：' . implode(', ', $items) . "\nid：" . $ids . "\n值：" . $values);
		
		return isset($_POST['pId']) ? msg(0, '修改成功！', array(
			'parent' => true
		)) : msg(0, '修改成功！');
	}
	
	// 删除
	function delete() {
		$tableName = 'admin_menu';
		if (empty($_POST['id']) || !preg_match('/^\s*\d+\s*(,\s*\d+\s*)*$/', $_POST['id']))
			return msg(1, '参数非法！');
		
		if (!$res = $this->sql->where("id IN ({$_POST['id']})")->delete($tableName))
			return msg(2, '删除失败！');
		
		$this->sql->increment($tableName);
		$this->log('删除菜单：' . $_POST['id']);
		
		return msg(0, '删除成功！', array(
			'url' => true
		));
	}
}